import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class Palette extends JPanel{
  private Vector<Pen> penList;
  private Pen currentPen;
  private int width;
  private JScrollPane inputPane;
  private JList graphicPenList;

  public Palette(int width){
    this.width = width;
    this.setBackground(Color.black);

    this.inputPane = new JScrollPane();

    penList = new Vector<Pen>();
    graphicPenList = new JList(penList);
    JScrollPane scrollingGraphicPenList = new JScrollPane(graphicPenList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    graphicPenList.setFixedCellHeight(width/3);
    graphicPenList.setCellRenderer(new PenCellRenderer());
    graphicPenList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    graphicPenList.addListSelectionListener(new PenSelectionListener(graphicPenList, this));

    JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, inputPane, scrollingGraphicPenList);

    this.setLayout(new GridLayout(1,1));
    this.add(split);
    
  }
  public Dimension getMinimumSize(){
  //  Dimension size = this.currentEasel.getSize();
   // size.width = this.width;
    return new Dimension(width,0);
  }
  public Dimension getPreferredSize(){
    //Dimension size = this.currentEasel.getSize();
    //size.width = this.width;
    return new Dimension(width,Integer.MAX_VALUE);
  }
  public Dimension getMaximumSize(){
    //Dimension size = this.currentEasel.getSize();
    //size.width = this.width;
    return new Dimension(width,Integer.MAX_VALUE);
  }
  public void addPen(Pen newPen){
    penList.add(newPen);
    newPen.setPalette(this);
  }
  public boolean hasCurrentPen(){
    return this.currentPen != null;
  }
  public Pen getCurrentPen(){
    return this.currentPen;
  }
  public void setCurrentPen(Pen currentPen){
    this.currentPen = currentPen;
    this.inputPane.setViewportView(currentPen.inputPanel);
  }
}

class PenSelectionListener implements ListSelectionListener{
  private JList graphicPenList;
  private Palette currentPalette;
  public PenSelectionListener(JList graphicPenList, Palette currentPalette){
    this.graphicPenList = graphicPenList;
    this.currentPalette = currentPalette;
  }
  public void valueChanged(ListSelectionEvent e){
    Pen selectedPen = (Pen) graphicPenList.getSelectedValue();
    selectedPen.select();
    this.currentPalette.setCurrentPen(selectedPen);
  }
}
        

class PenCellRenderer extends JLabel implements ListCellRenderer {
  // This is the only method defined by ListCellRenderer.
  // We just reconfigure the JLabel each time we're called.

  public Component getListCellRendererComponent(
  JList list,              // the list
  Object value,            // value to display
  int index,               // cell index
  boolean isSelected,      // is the cell selected
  boolean cellHasFocus)    // does the cell have focus
  {
    if(!(value instanceof Pen)){
      System.out.println("Error, getListCellRendererComponent method of PenCellRenderer needs a Pen as it's value");
      System.exit(0);
    }
    Pen pen = (Pen)value;
    setText(pen.getName());
    //make a buffered image of appropriate size here and then give it to the pen, and add it to this label as an icon.
    if (isSelected) {
      setBackground(list.getSelectionBackground());
      setForeground(list.getSelectionForeground());
    } else {
      setBackground(list.getBackground());
      setForeground(list.getForeground());
    }
    setEnabled(list.isEnabled());
    setFont(list.getFont());
    setOpaque(true);
    return this;
  }
}
